<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/5/16
 * Time: 下午12:27
 */

class Salesmanproduct extends Base_Controller
{
    public function __construct()
    {
        $this->_page_size = 20;
        $this->_form_type = 'salesman_product';
        $this->_list_where = array();
        $this->_action_list = array('EDIT');//EDIT,DEL,array('controller'=>'admin/commanswer/','function'=>'showlist','label'=>'查看回答')
        $this->_controller = 'admin/salesman/salesmanproduct/';//admin/user/
        parent::__construct();
    }

    public function showList($page = 1)
    {

        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $pdata = $this->getPagination($this->_controller.'showList/');

        $res = $this->getList($offset,$limit);
        //有创建按钮
        $createbtn = $this->load->view('admin/form/createbtn',array('controller'=>site_url($this->_controller)),true);
        //附加排序的js模块
        $filter = $this->load->view('admin/form/filter/kdtslsproductfilter',array('controller'=>site_url($this->_controller)),true);
        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'pagination'=>$pdata['pagination'],'createbtn'=>$createbtn,'filter'=>$filter);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content);
    }

    //重写获取list数据
    public function getList($offset = 0,$limit = 0)
    {
        $header = array("商品标识","封面","商品名称","商品价格","划线价","提成佣金","显示状态","操作");
        $this->load->model('kdtsalesman_model');
        $list = $this->kdtsalesman_model->getSalesmanProductListData($this->_controller,$this->_list_where,$limit,$offset);
        $res = array('headers'=>$header,'list'=>$list);
        return $res;
    }
    //url带1个以上参数或有get参数或重写了getlist方法重写分页方法
    public function getPagination($uri = '')
    {
        $this->db->from('mod_salesman_goods');
        if (!empty($this->_list_where)) $this->db->where($this->_list_where);
        $total = $this->db->count_all_results();

        $this->load->library('pagination');
        $config['base_url'] = site_url($uri);
        $config['total_rows'] = $total;
        $config['per_page'] = $this->_page_size;
        $config['suffix'] = '?'.$_SERVER['QUERY_STRING'];
        $config['first_url'] = $config['base_url'].'?'.$_SERVER['QUERY_STRING'];
        $config['cur_tag_open'] = '<div class="cur-page">';
        $config['cur_tag_close'] = '</div>';
        $config['use_page_numbers'] = TRUE;
        $config['num_tag_open'] = '<div class="pageitem">';
        $config['num_tag_close'] = '</div>';
        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<div class="pre-page">';
        $config['prev_tag_close'] = '</div>';
        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<div class="next-page">';
        $config['next_tag_close'] = '</div>';
        $config['last_link'] = '最后一页';
        $config['last_tag_open'] = '<div class="last-page">';
        $config['last_tag_close'] = '</div>';
        $config['first_link'] = '第一页';
        $config['first_tag_open'] = '<div class="first-page">';
        $config['first_tag_close'] = '</div>';
        $config['full_tag_open'] = '<div class="page-total-results">共'.$total.'条，每页'.$this->_page_size.'条</div>';

        $this->pagination->initialize($config);

        return array('total'=>$total,'pagination'=>$this->pagination->create_links());
    }

    public function update($id = 0)
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['num_iid']=trim($post['num_iid']);
            $post['title']=trim($post['title']);
            $post['price']=(float)trim($post['price'])*100;
            $post['priceUnderLine']=(float)trim($post['priceUnderLine'])*100;
            $post['i_rate']=(float)trim($post['i_rate'])*100;
            $res = $this->uploadImg('cover',800);
            if ($res['code'] == 1){
                $post['cover'] = $res['imgurl'][0];
            }
            $post['sort']=(int)trim($post['sort']);
            $this->_update_data = $post;
            parent::update($id);
        }
    }
    public function afterupdate($pkey,$pkeyval){}

    public function create()
    {
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->createBuildForm();
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['num_iid']=trim($post['num_iid']);
            $post['title']=trim($post['title']);
            $post['price']=(float)trim($post['price'])*100;
            $post['priceUnderLine']=(float)trim($post['priceUnderLine'])*100;
            $post['i_rate']=(float)trim($post['i_rate'])*100;
            $res = $this->uploadImg('cover',800);
            if ($res['code'] == 1){
                $post['cover'] = $res['imgurl'][0];
            }
            $post['sort']=(int)trim($post['sort']);
            $this->_create_data = $post;
            parent::create();
        }
    }

    public function editSort()
    {
        $get = $this->input->get();
        if (!isset($get['proid'])||!is_numeric($get['proid'])||
            !isset($get['sort'])||!is_numeric($get['sort']))
            $this->error();
        $proid = (int)$get['proid'];
        $sort = (int)$get['sort'];
        $this->load->model('kdtsalesman_model');
        $res = $this->kdtsalesman_model->sortEdit($proid,$sort,'mod_salesman_goods');
        if ($res===false) $this->error();
        else $this->success();
    }

    public function aftercreate($pkeyval)
    {
        /*$pkey为主键，$pkeyval为主键值*/
    }

    public function delete($id = 0,$externalid = 0)
    {
        $this->_external_id = $externalid;
        parent::delete($id);
    }

    public function afterdelete($pkey,$pkeyval){}
}